/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.sys.util.common;

import com.ibatis.dao.client.DaoManager;
import com.yunhoo.base.db.dao.DepartmentDao;
import com.yunhoo.base.db.dao.FinancialProjectDao;
import com.yunhoo.base.db.dao.ProjectDao;
import com.yunhoo.base.model.Department;
import com.yunhoo.base.model.FinancialProject;
import com.yunhoo.base.model.Iyear;
import com.yunhoo.base.model.Project;
import com.yunhoo.deptBudget.bean.FinBudgetAddBean;
import com.yunhoo.sys.db.config.DaoConfig;
import com.yunhoo.sys.db.util.IbatisTemplate;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.model.SelectItem;

/**
 *
 * @author Administrator
 */
public class CommonParam {

    private static DaoManager daom = null;
    private static DepartmentDao deptDao = null;
    private static FinancialProjectDao finProjDao = null;
    private static ProjectDao projDao = null;
    private static List<Iyear> yearList = null;
    private static Map<String,String> defaulValue = null;

    private static DaoManager getDaom() {
        if (daom == null) {
            daom = DaoConfig.getDaoManager();
        }
        return daom;
    }

    private static DepartmentDao getDeptDao() {
        if (deptDao == null) {
            deptDao = (DepartmentDao) getDaom().getDao(DepartmentDao.class);
        }
        return deptDao;
    }

    private static FinancialProjectDao getFinProjDao() {
        if (finProjDao == null) {
            finProjDao = (FinancialProjectDao) getDaom().getDao(FinancialProjectDao.class);
        }
        return finProjDao;
    }

    private static ProjectDao getProjDao() {
        if (projDao == null) {
            projDao = (ProjectDao) getDaom().getDao(ProjectDao.class);
        }
        return projDao;
    }

    private static List<Iyear> getYearList() {
        String orgCode = LoginParam.getOrg().getCode();
        yearList = IbatisTemplate.queryForList("iyear.queryListIyear", orgCode);
        System.out.println("yearList : " + yearList.size());
        return yearList;
    }

    public static List<SelectItem> yearList() {
        List<SelectItem> yearli = new ArrayList<SelectItem>();
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        System.out.println("currentYear : " + currentYear);
        for (Iyear iyear : getYearList()) {
            yearli.add(new SelectItem(iyear.getIyear(), iyear.getIyear() + "年"));
        }
        return yearli;
    }

    public static List<Integer> yearListOfInteger() {
        List<Integer> yearli = new ArrayList<Integer>();
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        System.out.println("currentYear : " + currentYear);
        for (Iyear iyear : getYearList()) {
            yearli.add(iyear.getIyear());
        }
        return yearli;
    }

    public static Integer defaultYear() {
        return Integer.parseInt(getDefaulValueMap().get("iyear"));
    }
    
    public static String defaulOrgCode(){
        return getDefaulValueMap().get("orgCode");
    }
    
    private static Map<String,String> getDefaulValueMap(){
        if(defaulValue == null){
            defaulValue = SysConfigMap.getSysConfigMap("defaultValue");
        }
        return defaulValue;
    }
    

    public static List<SelectItem> deptListOfSelect() {
        List<Department> templi = departmentList();
        List<SelectItem> deptli = new ArrayList<SelectItem>();
        for (Department tp : templi) {
            deptli.add(new SelectItem(tp.getCode(), tp.getName()));
        }
        return deptli;
    }

    public static List<Department> departmentList() {
        List<Department> templi = null;
        try {
            Department param = new Department(true);
            templi = getDeptDao().queryAll(param);
        } catch (SQLException ex) {
            Logger.getLogger(FinBudgetAddBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return templi;
    }

    public static List<SelectItem> finProjListOfSelect() {
        List<FinancialProject> templi = financialProjectList();
        List<SelectItem> deptli = new ArrayList<SelectItem>();
        for (FinancialProject tp : templi) {
            deptli.add(new SelectItem(tp.getFinCode(), tp.getFinName()));
        }
        return deptli;
    }

    public static List<FinancialProject> financialProjectList() {
        List<FinancialProject> templi = null;
        try {
            templi = getFinProjDao().queryAll(new FinancialProject(true));
        } catch (SQLException ex) {
            Logger.getLogger(CommonParam.class.getName()).log(Level.SEVERE, null, ex);
        }
        return templi;
    }

    public static List<SelectItem> projListOfSelect() {
        List<Project> templi = projectList();
        List<SelectItem> deptli = new ArrayList<SelectItem>();
        for (Project tp : templi) {
            deptli.add(new SelectItem(tp.getCode(), tp.getName()));
        }
        return deptli;
    }

    public static List<Project> projectList() {
        List<Project> templi = null;
        try {
            templi = getProjDao().queryAll(new Project(true));
        } catch (SQLException ex) {
            Logger.getLogger(CommonParam.class.getName()).log(Level.SEVERE, null, ex);
        }
        return templi;
    }
}
